class StarBackground {
    constructor() {
        this.canvas = document.createElement('canvas');
        this.ctx = this.canvas.getContext('2d');
        this.stars = [];
        this.numberOfStars = 200;
        this.shootingStars = [];
        this.maxShootingStars = 3;
        
        this.init();
    }

    init() {
        // 设置canvas为全屏
        this.canvas.style.position = 'fixed';
        this.canvas.style.top = '0';
        this.canvas.style.left = '0';
        this.canvas.style.width = '100%';
        this.canvas.style.height = '100%';
        this.canvas.style.zIndex = '0';
        this.canvas.style.pointerEvents = 'none';
        document.body.insertBefore(this.canvas, document.body.firstChild);

        // 调整canvas分辨率
        this.resize();
        window.addEventListener('resize', () => this.resize());

        // 初始化星星
        this.createStars();
        
        // 开始动画
        this.animate();
    }

    resize() {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
    }

    createStars() {
        this.stars = [];
        for (let i = 0; i < this.numberOfStars; i++) {
            this.stars.push({
                x: Math.random() * this.canvas.width,
                y: Math.random() * this.canvas.height,
                size: Math.random() * 2 + 1,
                speed: Math.random() * 0.5 + 0.1,
                brightness: Math.random(),
                angle: Math.random() * Math.PI * 2
            });
        }
    }

    createShootingStar() {
        if (this.shootingStars.length < this.maxShootingStars && Math.random() < 0.01) {
            this.shootingStars.push({
                x: Math.random() * this.canvas.width,
                y: 0,
                length: Math.random() * 80 + 100,
                speed: Math.random() * 10 + 10,
                angle: Math.PI / 4 + Math.random() * Math.PI / 4,
                opacity: 1
            });
        }
    }

    updateStars() {
        this.stars.forEach(star => {
            star.y += star.speed;
            star.brightness = Math.sin(Date.now() * 0.001 + star.angle) * 0.5 + 0.5;
            
            if (star.y > this.canvas.height) {
                star.y = 0;
                star.x = Math.random() * this.canvas.width;
            }
        });
    }

    updateShootingStars() {
        this.shootingStars = this.shootingStars.filter(star => {
            star.x += Math.cos(star.angle) * star.speed;
            star.y += Math.sin(star.angle) * star.speed;
            star.opacity -= 0.01;
            return star.opacity > 0;
        });
    }

    draw() {
        this.ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);

        // 绘制星星
        this.stars.forEach(star => {
            this.ctx.beginPath();
            this.ctx.fillStyle = `rgba(255, 255, 255, ${star.brightness})`;
            this.ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2);
            this.ctx.fill();
        });

        // 绘制流星
        this.shootingStars.forEach(star => {
            this.ctx.beginPath();
            const gradient = this.ctx.createLinearGradient(
                star.x, star.y,
                star.x - Math.cos(star.angle) * star.length,
                star.y - Math.sin(star.angle) * star.length
            );
            gradient.addColorStop(0, `rgba(255, 255, 255, ${star.opacity})`);
            gradient.addColorStop(1, 'rgba(0, 255, 255, 0)');
            
            this.ctx.strokeStyle = gradient;
            this.ctx.lineWidth = 2;
            this.ctx.moveTo(star.x, star.y);
            this.ctx.lineTo(
                star.x - Math.cos(star.angle) * star.length,
                star.y - Math.sin(star.angle) * star.length
            );
            this.ctx.stroke();
        });
    }

    animate() {
        this.updateStars();
        this.createShootingStar();
        this.updateShootingStars();
        this.draw();
        requestAnimationFrame(() => this.animate());
    }
}

// 创建背景实例
window.addEventListener('load', () => {
    new StarBackground();
}); 